//
//  AYStatusPictureView.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/20.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit
/// 最大列数
private let MaxColumn = 3


/// 图片间距
private let ImageViewMargin: CGFloat = 4

///图片宽高
private let AYStatusPictureViewImageViewWH = (UIScreen.mainScreen().bounds.width - 2 * CellMargin - (CGFloat(MaxColumn) - 1) * ImageViewMargin) /  CGFloat(MaxColumn)

class AYStatusPictureView: UIView {
    
    
    var pictureURLs: [NSURL]? {
        didSet{
            let size = layoutImages(pictureURLs)
//            AMPrint(items: "------------------------\(size)")
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)

            }
        }
    }
    
    ///拿到数据布局9个图片
    private func layoutImages(pictureURLs: [NSURL]?) -> CGSize{
        
        let count = pictureURLs?.count ?? 0
//                    AMPrint(items: "------------countcountcountcount------------\(count)")

//        deBugLabel.text = "\(count)"
        
        ///3列
        var numberOfColumn = 3
        
        if count == 1 {
            numberOfColumn = 1
        } else if count == 2 || count == 4 {
            numberOfColumn = 2
        }
        
        var index = 0
        
        ///遍历每一个imageView并设置frame 
        for imageView in imageViews {
            if index >= count {
                imageView.hidden = true
            } else {
                imageView.hidden = false
            ///图片所在的位置是哪一列
            let column = index % numberOfColumn
            ///图片所在的位置是哪一行
            let row = index / numberOfColumn
            
            let x = CGFloat(column) * (AYStatusPictureViewImageViewWH + ImageViewMargin)
            
            let y = CGFloat(row) * (AYStatusPictureViewImageViewWH + ImageViewMargin)
            
            imageView.frame = CGRect(x: x, y: y, width: AYStatusPictureViewImageViewWH, height: AYStatusPictureViewImageViewWH)

            ///拿到图片的url
            let url = pictureURLs![index]

            imageView.sd_setImageWithURL(url)
            }
            
            index += 1
        }
        
        //计算配图大小
        
        if count == 0 {
            return CGSizeZero
        }
        
        let numberOfRow = (count + numberOfColumn - 1) / numberOfColumn
        
        let width = CGFloat(numberOfColumn) * AYStatusPictureViewImageViewWH + CGFloat(numberOfColumn - 1) * CellMargin
        
        let height = CGFloat(numberOfRow) * AYStatusPictureViewImageViewWH + CGFloat(numberOfColumn - 1) * CellMargin
        
        return CGSize(width: width, height: height)

    }
    

    ///创建9个图片View的数组
    private var imageViews = [UIImageView]()
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
        
    }
    
    
    ///布局UI
    func prepareUI() {
        backgroundColor = UIColor.orangeColor()

        for _ in 0..<9 {
            let imageView = UIImageView()
            backgroundColor = UIColor.clearColor()
            
            imageViews.append(imageView)
            imageView.backgroundColor = UIColor.cyanColor()
            //设置图片的填充模式
            imageView.contentMode = UIViewContentMode.ScaleAspectFill
            imageView.clipsToBounds = true
            

            addSubview(imageView)
        }
        
//        addSubview(deBugLabel)
        
        
    }
    
    
    // MARK: - lazy
//    private var deBugLabel: UILabel = {
//       let label = UILabel()
//        label.font = UIFont.systemFontOfSize(20)
//        label.textColor = UIColor.blackColor()
//
//        label.text = "我还没有图片"
//        
//        label.frame = CGRect(x: 20, y: 20, width: 200, height: 100)
//        return label
//    }()
    
}
